/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * consultarCitas.java
 *
 * Created on 22-abr-2011, 17:17:03
 */

package interfaces;

import GestorCitas.Cita;
import GestorCitas.EstadoCita;
import GestorUsuarios.Usuario;
import clinica.ClinicaView;
import clinica.Interfaces;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import javax.swing.table.DefaultTableModel;
import org.jdesktop.application.Action;
import Controlador.*;
import clinica.Interfaz;
import java.text.SimpleDateFormat;
import javax.swing.ListSelectionModel;

/**
 *
 * @author victor
 */
public class consultarCitas extends javax.swing.JPanel {
    private ClinicaView app;
    private Cita [] cita;
    private Interfaces anterior;

    /** Creates new form consultarCitas */
    public consultarCitas(ClinicaView a) {
        app = a;
        initComponents();

        tabla.setRowHeight(32);
        tabla.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    }

    public void rellenaDatos(Cita [] cit, Interfaces a) throws SQLException, IOException, ClassNotFoundException, NoSuchAlgorithmException {
        cita = cit;
        anterior = a;
        DefaultTableModel m = (DefaultTableModel)tabla.getModel();

        while(m.getRowCount()>0)
            m.removeRow(0);

        for(Cita c:cita) {
            String estado = "";
            if(c.getEstado()==EstadoCita.CANCELADA)
                estado = "Cancelada";
            else if(c.getEstado()==EstadoCita.EN_CURSO)
                estado = "En curso";
            else if(c.getEstado()==EstadoCita.FINALIZADA)
                estado = "Finalizada";
            else if(c.getEstado()==EstadoCita.NO_LLEVADA_A_CABO)
                estado = "No llevada a cabo";
            else if(c.getEstado()==EstadoCita.PENDIENTE)
                estado = "Pendiente";

            Usuario paciente = Usuario.get(c.getIdPaciente());
            Usuario medico = Usuario.get(c.getIdMedico());

            SimpleDateFormat formato = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");

            String [] datos = {paciente.getNombre()+" "+paciente.getApellidos(),
            medico.getNombre()+" "+medico.getApellidos(),
            formato.format(c.getFechaCita()), estado};
            m.addRow(datos);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabla = new javax.swing.JTable();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        setName("Form"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(consultarCitas.class, this);
        jButton1.setAction(actionMap.get("aceptar")); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(consultarCitas.class);
        jButton1.setIcon(resourceMap.getIcon("jButton1.icon")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, resourceMap.getString("jPanel1.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, Interfaz.setFontSize())); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tabla.setFont(Interfaz.setFontSize());
        tabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Paciente", "Medico", "Fecha", "Estado"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabla.setName("tabla"); // NOI18N
        jScrollPane1.setViewportView(tabla);

        jButton2.setAction(actionMap.get("cancelar")); // NOI18N
        jButton2.setFont(Interfaz.setFontSize());
        jButton2.setIcon(resourceMap.getIcon("jButton2.icon")); // NOI18N
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        jButton3.setAction(actionMap.get("cambiarMed")); // NOI18N
        jButton3.setFont(Interfaz.setFontSize());
        jButton3.setIcon(resourceMap.getIcon("jButton3.icon")); // NOI18N
        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton2))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 720, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton3))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton1))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    @Action
    public void aceptar() {
        app.volver(anterior);
    }

    @Action
    public void cambiarMed() throws SQLException, IOException, ClassNotFoundException, NoSuchAlgorithmException {
        if(tabla.getSelectedRow()>=0) {
            if(cita[tabla.getSelectedRow()].getEstado() != EstadoCita.PENDIENTE)
                app.abrirMensajeError("La cita no está pendiente.", Interfaces.CONSULTAR_CITA);
            else app.abrirSeleccionarMedico(cita[tabla.getSelectedRow()]);
        }
    }

    @Action
    public void cancelar() throws SQLException, IOException, ClassNotFoundException, NoSuchAlgorithmException {
        if(tabla.getSelectedRow()>=0) {
            if(cita[tabla.getSelectedRow()].getEstado() != EstadoCita.PENDIENTE)
                app.abrirMensajeError("La cita no está pendiente.", Interfaces.CONSULTAR_CITA);
            else app.abrirCancelarCita(cita[tabla.getSelectedRow()]);
        }
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabla;
    // End of variables declaration//GEN-END:variables

}
